package com.sxpi.course.service.chapterlesson;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.course.mapper.ChapterLessonMapper;
import com.sxpi.course.pojo.ChapterLesson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/13 14:54
 */
@Service
@Slf4j
public class ChapterLessonServiceImpl extends ServiceImpl<ChapterLessonMapper, ChapterLesson> implements IChapterLessonService {

    /**
     * 根据课程id查询对应章节的信息
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterLesson> getChapterLessonDetail(Long courseId){
        // 查询所以章节，然后用stream进行整理
        LambdaQueryWrapper<ChapterLesson> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(
            ChapterLesson::getId,
            ChapterLesson::getTitle,
            ChapterLesson::getPid,
            ChapterLesson::getCreateTime,
            ChapterLesson::getUpdateTime,
            ChapterLesson::getSorted,
            ChapterLesson::getIsfree,
            ChapterLesson::getCourseid,
            ChapterLesson::getCoursetimer,
            ChapterLesson::getCoursetimersize,
            ChapterLesson::getDescription
        );
        lambdaQueryWrapper.eq(ChapterLesson::getStatus,1);
        lambdaQueryWrapper.eq(ChapterLesson::getIsdelete,0);
        lambdaQueryWrapper.eq(ChapterLesson::getCourseid,courseId);

        // 查询所以的章节数据
        List<ChapterLesson> chapterLessonListAlls = this.list(lambdaQueryWrapper);
        List<ChapterLesson> chapterList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(chapterLessonListAlls)) {
            // 过滤章出来（全部的章）
             chapterList = chapterLessonListAlls.stream()
                    .filter(chapter -> chapter.getPid().equals(0L))
                    .sorted((a,b)->a.getSorted()-b.getSorted())
                    .collect(Collectors.toList());

            // 遍历章对应章的节进行挂载
            if (!CollectionUtils.isEmpty(chapterList)){
                chapterList.forEach(chapter->{
                    List<ChapterLesson> lessonList = chapterLessonListAlls.stream()
                            .filter(chapterLesson -> chapterLesson.getPid().equals(chapter.getId()))
                            .sorted((a,b)->a.getSorted()-b.getSorted())
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(lessonList)){
                        chapter.setChildrens(lessonList);
                    }else{
                        // 代表该章，没有小结信息
                        chapter.setChildrens(new ArrayList<>());
                    }
                });
            }
        }
        return chapterList;
    }

    /**
     * 根据课程id查询对应章节的信息（去除空字段）
     * @param courseId
     * @return
     */
    @Override
    public List<Map<String,Object>> getChapterLessonDetailMap(Long courseId){
        // 查询所以的章节数据
        List<Map<String, Object>> chapterLessonListAlls = this.baseMapper.findChapterLessons(courseId);
        List<Map<String, Object>> chapterList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(chapterLessonListAlls)) {
            // 过滤章出来
             chapterList = chapterLessonListAlls.stream()
                    .filter(chapter -> Long.parseLong(String.valueOf(chapter.get("pid")))==0L)
                    .sorted((a,b)->Integer.parseInt(String.valueOf(a.get("sorted")))
                             -Integer.parseInt(String.valueOf(b.get("sorted"))))
                    .collect(Collectors.toList());

            // 遍历章对应章的节进行挂载
            if (!CollectionUtils.isEmpty(chapterList)){
                chapterList.forEach(chapter->{
                    List<Map<String, Object>> lessonList = chapterLessonListAlls.stream()
                            .filter(chapterLesson -> Long.parseLong(String.valueOf(chapterLesson.get("pid"))) == (Long.parseLong(String.valueOf(chapter.get("id")))))
                            .sorted((a,b)->Integer.parseInt(String.valueOf(a.get("sorted")))
                                    -Integer.parseInt(String.valueOf(b.get("sorted"))))
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(lessonList)){
                        chapter.put("childrens",lessonList);
                    }else{
                        // 代表该章，没有小结信息
                        chapter.put("childrens",new ArrayList<>());
                    }
                });
            }
        }
        return chapterList;
    }

}
